$(warning 'tomcat.out')

TOMCAT := $(HOME)/tomcat


tomcat.out: java.out /usr/lib/systemd/system/tomcat.service
	echo $(TOMCAT)
	touch tomcat.out


/usr/lib/systemd/system/tomcat.service: $(TOMCAT)/tomcat.service
	rm -rf $@
	ln -s $< $@


$(TOMCAT)/tomcat.service: $(TOMCAT)/bin/setenv.sh
	rm -rf $@
	echo "[Unit]" >> $@
	echo "Description=Tomcat" >> $@
	echo "After=syslog.target network.target remote-fs.target nss-lookup.target" >> $@
	echo "" >> $@
	echo "[Service]" >> $@
	echo "Type=forking" >> $@
	echo "PIDFile=$(TOMCAT)/tomcat.pid" >> $@
	echo "ExecStart=$(TOMCAT)/bin/startup.sh" >> $@
	echo "ExecReload=/bin/kill -s HUP "'$$'"MAINPID" >> $@
	echo "ExecStop=/bin/kill -s QUIT "'$$'"MAINPID" >> $@
	echo "PrivateTmp=true" >> $@
	echo "" >> $@
	echo "[Install]" >> $@
	echo "WantedBy=multi-user.target" >> $@
	-systemctl disable tomcat
	systemctl enable tomcat
	systemctl restart tomcat


$(TOMCAT)/bin/setenv.sh: apache-tomcat-9.0.1.tar.gz
	cd $(HOME); tar zxf $(PWD)/$<; ln -s apache-tomcat-9.0.1 tomcat
	echo "#add tomcat pid" >> $@
	echo "CATALINA_PID=\""'$$'"CATALINA_BASE/tomcat.pid\"" >> $@
	echo "#add java opts" >> $@
	echo "JAVA_OPTS=\"-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m\"" >> $@
	echo "" >> $@
	chmod +x $@	


apache-tomcat-9.0.1.tar.gz:
	wget http://mirrors.shuosc.org/apache/tomcat/tomcat-9/v9.0.1/bin/apache-tomcat-9.0.1.tar.gz


